package com.ruoyi.generator.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.generator.domain.AppBuildInfo;
import com.ruoyi.generator.domain.vo.AppPageTree;
import com.ruoyi.generator.service.IAppBuildInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.generator.domain.FromBuildPage;
import com.ruoyi.generator.service.IFromBuildPageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 移动端页面树Controller
 *
 * @author ZGL
 * @date 2024-12-12
 */
@RestController
@RequestMapping("/generator/FromBuildPage")
public class FromBuildPageController extends BaseController
{
    @Autowired
    private IFromBuildPageService fromBuildPageService;

    @Autowired
    private IAppBuildInfoService appBuildInfoService;

    /**
     * 查询移动端页面树列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FromBuildPage fromBuildPage)
    {
        startPage();
        List<FromBuildPage> list = fromBuildPageService.selectFromBuildPageList(fromBuildPage);
        return getDataTable(list);
    }

    /**
     * 导出移动端页面树列表
     */
    @Log(title = "移动端页面树", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FromBuildPage fromBuildPage)
    {
        List<FromBuildPage> list = fromBuildPageService.selectFromBuildPageList(fromBuildPage);
        ExcelUtil<FromBuildPage> util = new ExcelUtil<FromBuildPage>(FromBuildPage.class);
        util.exportExcel(response, list, "移动端页面树数据");
    }

    /**
     * 获取移动端页面树详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(fromBuildPageService.selectFromBuildPageById(id));
    }

    /**
     * 新增移动端页面树
     */
    @Log(title = "移动端页面树", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FromBuildPage fromBuildPage)
    {
        return toAjax(fromBuildPageService.insertFromBuildPage(fromBuildPage));
    }

    /**
     * 修改移动端页面树
     */
    @Log(title = "移动端页面树", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody FromBuildPage fromBuildPage)
    {
        return toAjax(fromBuildPageService.updateFromBuildPage(fromBuildPage));
    }
    /**
     * 修改移动端页面名称
     */
    @Log(title = "移动端页面树", businessType = BusinessType.UPDATE)
    @PostMapping("/editName")
    public AjaxResult editName(@RequestBody FromBuildPage fromBuildPage)
    {
        return toAjax(fromBuildPageService.updateFromBuildPage(fromBuildPage));
    }

    /**
     * 删除移动端页面树
     */
    @Log(title = "移动端页面树", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fromBuildPageService.deleteFromBuildPageByIds(ids));
    }

    @Log(title = "获取移动端页面树")
    @GetMapping("/getFromBuildPageTree")
    public AjaxResult getFromBuildPageTree(@RequestParam String appKey)
    {
        ArrayList<AppPageTree> retList = new ArrayList<>();
        retList.add(fromBuildPageService.getFromBuildPageTree(appKey));
        HashMap<String, Object> retMap = new HashMap<>();
        retMap.put("tree",retList);
        AppBuildInfo appBuildInfo = new AppBuildInfo();
        appBuildInfo.setAppKey(appKey);
        appBuildInfo.setStatus("0");
        List<AppBuildInfo> appBuildInfos = appBuildInfoService.selectAppBuildInfoList(appBuildInfo);
        if(appBuildInfos.size() > 0){
            retMap.put("appInfo",appBuildInfos.get(0));
        }else{
            retMap.put("appInfo",null);
        }
        return success(retMap);
    }
    @Log(title = "根据appKey获取移动端首页")
    @GetMapping("/getAppHomePageByAppKey")
    public AjaxResult getAppHomePageByAppKey(@RequestParam String appKey)
    {
        FromBuildPage fromBuildPage = new FromBuildPage();
        fromBuildPage.setAppKey(appKey);
        fromBuildPage.setStatus("0");
        fromBuildPage.setParentId("0");
        List<FromBuildPage> fromBuildPages = fromBuildPageService.selectFromBuildPageList(fromBuildPage);
        if(fromBuildPages!= null){
            fromBuildPage.setParentId(fromBuildPages.get(0).getId().toString());
            fromBuildPages = fromBuildPageService.selectFromBuildPageList(fromBuildPage);
            return AjaxResult.success("成功", fromBuildPages.get(0).getBuildKey());
        }else{
            return error("未找到首页");
        }
    }

}
